package cn.edu.sjtu.se.dslab.auth;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import cn.edu.sjtu.se.dslab.project.PGroup;

/**
 * user class
 * 
 * @author smile
 * 
 */

@Entity
@Table(name = "TB_USER")
public class User implements Serializable {

	private static final long serialVersionUID = 1292424748818673960L;

	// Properties
	// user id
	private long id;
	
	// login name
	private String username;
	
	// login password
	private String password;
	
	private String realname;
	
	private String staffNum; 
	
	// email
	private String email;
	
	// groups
	private Set<Group> groupsList = new HashSet<Group>();
	
	// permissions and urls user can access
	private Set<Perm> permsList = new HashSet<Perm>();

	private Set<PGroup> pGroups = new HashSet<PGroup>(); 
	
	
	/**
	 * Constructor
	 */
	public User() {
	}

	/**
	 * retrieve user id
	 * 
	 * @return long
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public long getId() {
		return id;
	}

	/**
	 * set user id
	 * 
	 * @param id
	 *            user id
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * get login name
	 * 
	 * @return String
	 */
	@Column(name = "username",length = 32, unique = true, nullable = false, updatable = false)
	public String getUsername() {
		return username;
	}

	/**
	 * set login name
	 * 
	 * @param username
	 *            login name
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * retrieve user password
	 * 
	 * @return String
	 */
	@Column(name = "password",nullable = false, length = 60, updatable = true)
	public String getPassword() {
		return password;
	}

	/**
	 * set login password
	 * 
	 * @param password
	 *            login password
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	@Column(name = "realname",nullable = true, length = 60, updatable = true)
	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}
	
	@Column(name = "staffNum",nullable = true, length = 60, updatable = true)
	public String getStaffNum() {
		return staffNum;
	}

	public void setStaffNum(String staffNum) {
		this.staffNum = staffNum;
	}

	/**
	 * get email
	 * 
	 * @return String
	 */
	@Column(length = 128)
	public String getEmail() {
		return email;
	}

	/**
	 * set email
	 * 
	 * @param email
	 *            email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	@Fetch(FetchMode.SUBSELECT)
	@ManyToMany(targetEntity = Group.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "TB_GROUP_USER", joinColumns = { @JoinColumn(name = "FK_USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "FK_GROUP_ID") })
	public Set<Group> getGroupsList() {
		return groupsList;
	}

	public void setGroupsList(Set<Group> groupsList) {
		this.groupsList = groupsList;
	}

	@Fetch(FetchMode.SUBSELECT)
	@ManyToMany(targetEntity = Perm.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "TB_PERM_USER", joinColumns = { @JoinColumn(name = "FK_USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "FK_PERM_ID") })
	public Set<Perm> getPermsList() {
		return permsList;
	}

	public void setPermsList(Set<Perm> permsList) {
		this.permsList = permsList;
	}
	
	
	@Fetch(FetchMode.SUBSELECT)
	@ManyToMany(targetEntity = PGroup.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "user_pgroup", joinColumns = { @JoinColumn(name = "fk_user_id") }, inverseJoinColumns = { @JoinColumn(name = "fk_pgroup_id") })	
	public Set<PGroup> getpGroups() {
		return pGroups;
	}

	public void setpGroups(Set<PGroup> pGroups) {
		this.pGroups = pGroups;
	}

	@Override
	public String toString() {
		return username;
	}

//	@Override
//	public int hashCode() {
//		final int prime = 31;
//		int result = 1;
//		result = prime * result + (int) (id ^ (id >>> 32));
//		return result;
//	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	

}
